package com.xin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xin.entity.*;
import com.xin.mapper.SDeptProfessionalDao;
import com.xin.mapper.UStudentInfoDao;
import com.xin.mapper.UStudentJiBenXinXiDao;
import com.xin.mapper.UStudentXueXiChengJiDao;
import com.xin.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xin.util.TeacherLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学生信息 服务实现类
 * </p>
 *
 * @author l1
 * @since 2017-11-14
 */
@Service
public class UStudentInfoServiceImpl extends ServiceImpl<UStudentInfoDao, UStudentInfo> implements UStudentInfoService {

    @Value("${school.name}")
    private String schoolName;

    @Autowired
    private UStudentXueXiChengJiDao studentXueXiChengJiDao;
    @Autowired
    private SDeptProfessionalDao sDeptProfessionalDao;

    @Autowired
    private UStudentJiBenXinXiService uStudentJiBenXinXiService;

    @Autowired
    private UStudentJtcySsgxService uStudentJtcySsgxService;

    @Autowired
    private UStudentZhengZhiMianMaoService uStudentZhengZhiMianMaoService;

    @Autowired
    private UStudentXueXiChengJiService uStudentXueXiChengJiService;

    @Autowired
    private UStudentZongHeCePingService uStudentZongHeCePingService;

    @Autowired
    private UStudentJiangLiService uStudentJiangLiService;

    @Autowired
    private UStudentZiZhuService uStudentZiZhuService;

    @Autowired
    private UStudentRongYuService uStudentRongYuService;

    @Autowired
    private UStudentChuFenService uStudentChuFenService;

    @Autowired
    private UStudentXueTuanGanBuService uStudentXueTuanGanBuService;

    @Autowired
    private UStudentSheHuiGongZuoService uStudentSheHuiGongZuoService;

    @Autowired
    private UStudentZhengShuKaoShiService uStudentZhengShuKaoShiService;

    @Autowired
    private UStudentKeJiChengGuoService uStudentKeJiChengGuoService;

    @Autowired
    private UStudentCaiWuService uStudentCaiWuService;

    @Autowired
    private UStudentTuShuJieYueService uStudentTuShuJieYueService;

    @Autowired
    private UStudentXiaoYuanKaXiaoFeiService uStudentXiaoYuanKaXiaoFeiService;

    @Autowired
    private UStudentXykXiaoFeiXiangQingService uStudentXykXiaoFeiXiangQingService;

    @Autowired
    private UStudentShangWangJiLuService uStudentShangWangJiLuService;

    @Autowired
    private SOrgService sOrgService;

    @Override
    public void index(Model model, List<SOrg> orgs) {
        //校长
        UStudentInfo uStudentInfo = new UStudentInfo();
        List<String> twoList = new ArrayList<>();
//        Map<String,List<String>> threeMap= new HashMap<>();
        orgs.forEach(sOrg -> {
            Integer level = sOrg.getLevel();
            String sOrgName = sOrg.getName();

            if(level == TeacherLevel.ONE.getLevel()){
                List<SOrg> childOrges = sOrgService.selectList(new EntityWrapper<SOrg>().eq("parent_id", sOrg.getId()));
                childOrges.forEach(childOrg->{
                    if(!twoList.contains(childOrg.getName())){
                        twoList.add(childOrg.getName());
                    }
                });
            }else  if(level == TeacherLevel.TWO.getLevel()){
                twoList.add(sOrgName);
            }
//            else  if(level == TeacherLevel.THREE.getLevel()){
//                SOrg twoOrg = sOrgService.selectById(sOrg.getParentId());
//                String twoOrgName = twoOrg.getName();
//                List<String> threeList = threeMap.getOrDefault(twoOrgName, new ArrayList<>());
//                threeList.add(sOrgName);
//                threeMap.put(twoOrgName,threeList);
//            }

        });

        model.addAttribute("key",schoolName);

        if(twoList.isEmpty()) {
            model.addAttribute("studentDetail","studentDetail");
            return;
        }
        // 查询所有
        JSONObject allJSON = new JSONObject();
        allJSON.put("zaiXiaoBenKeSheng",0);
        allJSON.put("zaiXiaoYanJiuSheng",0);
        allJSON.put("biYeBenKeSheng",0);
        allJSON.put("biYeYanJiuSheng",0);

        if(!twoList.isEmpty()){
            //在校本科生
            uStudentInfo.setStatus("在校");
            uStudentInfo.setEducation("本科");
            Integer zaiXiaoBenKeSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo).in("subject_attribution",twoList));
            allJSON.put("zaiXiaoBenKeSheng",zaiXiaoBenKeSheng+allJSON.getInteger("zaiXiaoBenKeSheng"));
//        model.addAttribute("zaiXiaoBenKeSheng",zaiXiaoBenKeSheng);
            //在校研究生
            uStudentInfo.setEducation("研究生");
            Integer zaiXiaoYanJiuSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo).in("subject_attribution",twoList));
            allJSON.put("zaiXiaoYanJiuSheng",zaiXiaoYanJiuSheng+allJSON.getInteger("zaiXiaoYanJiuSheng"));
//        model.addAttribute("zaiXiaoYanJiuSheng",zaiXiaoYanJiuSheng);
            //毕业本科生
            uStudentInfo.setStatus("毕业");
            uStudentInfo.setEducation("本科");
            Integer biYeBenKeSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo).in("subject_attribution",twoList));
            allJSON.put("biYeBenKeSheng",biYeBenKeSheng+allJSON.getInteger("biYeBenKeSheng"));
//        model.addAttribute("biYeBenKeSheng",biYeBenKeSheng);
            //毕业研究生
            uStudentInfo.setStatus("毕业");
            uStudentInfo.setEducation("研究生");
            Integer biYeYanJiuSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo).in("subject_attribution",twoList));
            allJSON.put("biYeYanJiuSheng",biYeYanJiuSheng+allJSON.getInteger("biYeYanJiuSheng"));
//        model.addAttribute("biYeYanJiuSheng",biYeYanJiuSheng);

        }

        List<JSONObject> list = new ArrayList<>();
        if(!twoList.isEmpty()) {
            //查询所有院系  二级目录
            twoList.forEach(collegeName -> {
                JSONObject itemJSON = new JSONObject();
                uStudentInfo.setStatus(null);
                uStudentInfo.setEducation(null);
                uStudentInfo.setSubjectAttribution(collegeName);
                Integer collegeNum = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
                if(collegeNum != 0){
                    itemJSON.put("num", collegeNum);
                    itemJSON.put("name", collegeName);


                    uStudentInfo.setStatus("在校");
                    uStudentInfo.setEducation("本科");
                    uStudentInfo.setSubjectAttribution(collegeName);
                    Integer yZaiXiaoBenKeSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
                    itemJSON.put("yZaiXiaoBenKeSheng", yZaiXiaoBenKeSheng);

                    uStudentInfo.setStatus("在校");
                    uStudentInfo.setEducation("研究生");
                    uStudentInfo.setSubjectAttribution(collegeName);
                    Integer yZaiXiaoYanJiuSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
                    itemJSON.put("yZaiXiaoYanJiuSheng", yZaiXiaoYanJiuSheng);

                    uStudentInfo.setStatus("毕业");
                    uStudentInfo.setEducation("本科");
                    uStudentInfo.setSubjectAttribution(collegeName);
                    Integer yBiYeBenKeSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
                    itemJSON.put("yBiYeBenKeSheng", yBiYeBenKeSheng);

                    uStudentInfo.setStatus("毕业");
                    uStudentInfo.setEducation("研究生");
                    uStudentInfo.setSubjectAttribution(collegeName);
                    Integer yBiYeYanJiuSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
                    itemJSON.put("yBiYeYanJiuSheng", yBiYeYanJiuSheng);

                    list.add(itemJSON);
                }

            });

//            threeMap.keySet().forEach(key -> {
//                List<String> professionList = threeMap.get(key);
//                //在校本科生
//                uStudentInfo.setStatus("在校");
//                uStudentInfo.setEducation("本科");
//                uStudentInfo.setSubjectAttribution(key);
//                Integer zaiXiaoBenKeSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo).in("prefessional_attribution", professionList));
//                allJSON.put("zaiXiaoBenKeSheng", zaiXiaoBenKeSheng + allJSON.getInteger("zaiXiaoBenKeSheng"));
//                //在校研究生
//                uStudentInfo.setEducation("研究生");
//                Integer zaiXiaoYanJiuSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo).in("prefessional_attribution", professionList));
//                allJSON.put("zaiXiaoYanJiuSheng", zaiXiaoYanJiuSheng + allJSON.getInteger("zaiXiaoYanJiuSheng"));
//                //毕业本科生
//                uStudentInfo.setStatus("毕业");
//                uStudentInfo.setEducation("本科");
//                Integer biYeBenKeSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo).in("prefessional_attribution", professionList));
//                allJSON.put("biYeBenKeSheng", biYeBenKeSheng + allJSON.getInteger("biYeBenKeSheng"));
//                //毕业研究生
//                uStudentInfo.setStatus("毕业");
//                uStudentInfo.setEducation("研究生");
//                Integer biYeYanJiuSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo).in("prefessional_attribution", professionList));
//                allJSON.put("biYeYanJiuSheng", biYeYanJiuSheng + allJSON.getInteger("biYeYanJiuSheng"));
//
//                professionList.forEach(professionalName -> {
//                    JSONObject itemJSON = new JSONObject();
//                    uStudentInfo.setStatus(null);
//                    uStudentInfo.setEducation(null);
//                    uStudentInfo.setPrefessionalAttribution(professionalName);
//                    Integer num = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
//                    if(num!=0){
//                        itemJSON.put("num", num);
//                        itemJSON.put("name", professionalName);
//
//                        uStudentInfo.setStatus("在校");
//                        uStudentInfo.setEducation("本科");
//                        uStudentInfo.setPrefessionalAttribution(professionalName);
//                        Integer yZaiXiaoBenKeSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
//                        itemJSON.put("yZaiXiaoBenKeSheng", yZaiXiaoBenKeSheng);
//
//                        uStudentInfo.setStatus("在校");
//                        uStudentInfo.setEducation("研究生");
//                        uStudentInfo.setPrefessionalAttribution(professionalName);
//                        Integer yZaiXiaoYanJiuSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
//                        itemJSON.put("yZaiXiaoYanJiuSheng", yZaiXiaoYanJiuSheng);
//
//                        uStudentInfo.setStatus("毕业");
//                        uStudentInfo.setEducation("本科");
//                        uStudentInfo.setPrefessionalAttribution(professionalName);
//                        Integer yBiYeBenKeSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
//                        itemJSON.put("yBiYeBenKeSheng", yBiYeBenKeSheng);
//
//                        uStudentInfo.setStatus("毕业");
//                        uStudentInfo.setEducation("研究生");
//                        uStudentInfo.setPrefessionalAttribution(professionalName);
//                        Integer yBiYeYanJiuSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
//                        itemJSON.put("yBiYeYanJiuSheng", yBiYeYanJiuSheng);
//
//                        list.add(itemJSON);
//                    }
//                });
//            });


            //在校本科生
            model.addAttribute("zaiXiaoBenKeSheng",allJSON.getInteger("zaiXiaoBenKeSheng"));
            //在校研究生
            model.addAttribute("zaiXiaoYanJiuSheng",allJSON.getInteger("zaiXiaoYanJiuSheng"));
            //毕业本科生
            model.addAttribute("biYeBenKeSheng",allJSON.getInteger("biYeBenKeSheng"));
            //毕业研究生
            model.addAttribute("biYeYanJiuSheng",allJSON.getInteger("biYeYanJiuSheng"));

            model.addAttribute("colleges", list);


//        if(level == TeacherLevel.THREE.getLevel()){
//         //系
//            model.addAttribute("key",profession);
//            //在校本科生
//            uStudentInfo.setStatus("在校");
//            uStudentInfo.setEducation("本科");
//            uStudentInfo.setSubjectAttribution(college);
//            uStudentInfo.setPrefessionalAttribution(profession);
//            Integer zaiXiaoBenKeSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
//            model.addAttribute("zaiXiaoBenKeSheng",zaiXiaoBenKeSheng);
//            //在校研究生
//            uStudentInfo.setEducation("研究生");
//            Integer zaiXiaoYanJiuSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
//            model.addAttribute("zaiXiaoYanJiuSheng",zaiXiaoYanJiuSheng);
//            //毕业本科生
//            uStudentInfo.setStatus("毕业");
//            uStudentInfo.setEducation("本科");
//            Integer biYeBenKeSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
//            model.addAttribute("biYeBenKeSheng",biYeBenKeSheng);
//            //毕业研究生
//            uStudentInfo.setStatus("毕业");
//            uStudentInfo.setEducation("研究生");
//            Integer biYeYanJiuSheng = baseMapper.selectCount(new EntityWrapper<>(uStudentInfo));
//            model.addAttribute("biYeYanJiuSheng",biYeYanJiuSheng);
//
//            List<JSONObject> list = new ArrayList<>();
//            JSONObject itemJSON = new JSONObject();
//            uStudentInfo.setPrefessionalAttribution(profession);
//            uStudentInfo.setStatus(null);
//            uStudentInfo.setEducation(null);
//            itemJSON.put("num",baseMapper.selectCount(new EntityWrapper<>(uStudentInfo)));
//            itemJSON.put("name",profession);
//            itemJSON.put("yZaiXiaoBenKeSheng",zaiXiaoBenKeSheng);
//            itemJSON.put("yZaiXiaoYanJiuSheng",zaiXiaoYanJiuSheng);
//            itemJSON.put("yBiYeBenKeSheng",biYeBenKeSheng);
//            itemJSON.put("yBiYeYanJiuSheng",biYeYanJiuSheng);
//            list.add(itemJSON);
//            model.addAttribute("colleges",list);
//        }
        }
    }

    @Override
    public void findDetail(String id, Model model) {
        //detail
        UStudentInfo uStudentInfo = this.selectOne(new EntityWrapper<UStudentInfo>().eq("id", id));
        model.addAttribute("uStudentInfo",uStudentInfo);

        //基本信息
        List<UStudentJiBenXinXi> jiBenXinXis = uStudentJiBenXinXiService.selectList(new EntityWrapper<UStudentJiBenXinXi>().eq("sid", id));
        List<UStudentJtcySsgx> jtcySsgxes = uStudentJtcySsgxService.selectList(new EntityWrapper<UStudentJtcySsgx>().eq("sid", id));
        model.addAttribute("jiBenXinXis",jiBenXinXis);
        model.addAttribute("jtcySsgxes",jtcySsgxes);

        //政治面貌
        List<UStudentZhengZhiMianMao> zhengZhiMianMaos = uStudentZhengZhiMianMaoService.selectList(new EntityWrapper<UStudentZhengZhiMianMao>().eq("sid", id));
        model.addAttribute("zhengZhiMianMaos",zhengZhiMianMaos);
        //学习成绩
        List<JSON> xueXiChengJies = studentXueXiChengJiDao.findChengJiById(id);
        model.addAttribute("xueXiChengJies",xueXiChengJies);

        //综合测评
        List<UStudentZongHeCePing> zongHeCePings = uStudentZongHeCePingService.selectList(new EntityWrapper<UStudentZongHeCePing>().eq("sid", id));
        model.addAttribute("zongHeCePings",zongHeCePings);

        //奖励
        List<UStudentJiangLi> jiangLis = uStudentJiangLiService.selectList(new EntityWrapper<UStudentJiangLi>().eq("sid", id));
        model.addAttribute("jiangLis",jiangLis);

        //资助
        List<UStudentZiZhu> ziZhus = uStudentZiZhuService.selectList(new EntityWrapper<UStudentZiZhu>().eq("sid", id));
        model.addAttribute("ziZhus",ziZhus);


        //荣誉
        List<UStudentRongYu> rongYus = uStudentRongYuService.selectList(new EntityWrapper<UStudentRongYu>().eq("sid", "id"));
        model.addAttribute("rongYus",rongYus);


        //处分
        List<UStudentChuFen> chuFens = uStudentChuFenService.selectList(new EntityWrapper<UStudentChuFen>().eq("sid", "id"));
        model.addAttribute("chuFens",chuFens);

        //学团干部
        List<UStudentXueTuanGanBu> xueTuanGanBus = uStudentXueTuanGanBuService.selectList(new EntityWrapper<UStudentXueTuanGanBu>().eq("sid", id));
        model.addAttribute("xueTuanGanBus",xueTuanGanBus);

        //社会工作
        List<UStudentSheHuiGongZuo> sheHuiGongZuos = uStudentSheHuiGongZuoService.selectList(new EntityWrapper<UStudentSheHuiGongZuo>().eq("sid", id));
        model.addAttribute("sheHuiGongZuos",sheHuiGongZuos);


        //证书考试
        List<UStudentZhengShuKaoShi> zhengShuKaoShis = uStudentZhengShuKaoShiService.selectList(new EntityWrapper<UStudentZhengShuKaoShi>().eq("sid", id));
        model.addAttribute("zhengShuKaoShis",zhengShuKaoShis);

        //科技成果
        List<UStudentKeJiChengGuo> keJiChengGuos = uStudentKeJiChengGuoService.selectList(new EntityWrapper<UStudentKeJiChengGuo>().eq("sid", id));
        model.addAttribute("keJiChengGuos",keJiChengGuos);

        //财务
        List<UStudentCaiWu> caiWus = uStudentCaiWuService.selectList(new EntityWrapper<UStudentCaiWu>().eq("sid", id));
        model.addAttribute("caiWus",caiWus);

        //图书借阅
        List<UStudentTuShuJieYue> tuShuJieYues = uStudentTuShuJieYueService.selectList(new EntityWrapper<UStudentTuShuJieYue>().eq("sid", id));
        model.addAttribute("tuShuJieYues",tuShuJieYues);


        //校园卡消费
        List<UStudentXiaoYuanKaXiaoFei> xiaoYuanKaXiaoFeis = uStudentXiaoYuanKaXiaoFeiService.selectList(new EntityWrapper<UStudentXiaoYuanKaXiaoFei>().eq("sid", id));
        model.addAttribute("xiaoYuanKaXiaoFeis",xiaoYuanKaXiaoFeis);


        //上网记录
        List<UStudentShangWangJiLu> shangWangJiLus = uStudentShangWangJiLuService.selectList(new EntityWrapper<UStudentShangWangJiLu>().eq("sid", id));
        model.addAttribute("shangWangJiLus",shangWangJiLus);


    }
}
